<template>
  <div>
    <h3 style="margin: 10px 0;">{{ type == 2 ? '攻击' : '防御' }}报告列表</h3>
    <Table border :columns="defeFileCol" :data="row"></Table>
  </div>
</template>
<script>
export default {
  props: {
    row: Array,
    type: Number
  },
  data() {
    return {
      defeFileCol: [
        {
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '名称',
          key: 'name'
        },
        {
          title: '创建者',
          key: 'creatorName'
        },
        {
          title: '用户类型',
          key: 'userType'
        },
        // {
        //   title: '报告类型',
        //   key: 'fileType',
        //   render: (h, params) => {
        //     let text = '';
        //     if (params.row.fileType === 'ORIGINAL') {
        //       text = '原报告';
        //     } else if (params.row.fileType === 'SUPPLEMENT') {
        //       text = '补充报告';
        //     }
        //     return h('div', text);
        //   }
        // },
        {
          title: '操作',
          key: 'action',
          width: 140,
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.downFile(params.row);
                    }
                  }
                },
                '下载'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginLeft: '5px'
                  },
                  on: {
                    click: () => {
                      //新的预览接口
                      window.openTrain(`/api/minio/filePreview?id=${params.row.id}`);
                    }
                  }
                },
                '预览'
              )
            ]);
          }
        }
      ]
    };
  },
  created() {},
  mounted() {},
  methods: {
    downFile(row) {
      // let windowsUrl = window.location.href;
      let windowsUrl = window.location.origin;
      windowsUrl = windowsUrl.split('//');
      let getIp = windowsUrl[1].split('/')[0];
      // getIp = getIp.split(':')[0];
      var objurl = `${getIp}/Learn/${row.path}`;
      // 处理下载地址与当前网站不同源，download属性不生效的问题
      const x = new window.XMLHttpRequest();
      x.open('GET', `http://${objurl}`, true);
      x.responseType = 'blob';
      x.onload = () => {
        const url = window.URL.createObjectURL(x.response);
        const a = document.createElement('a');
        a.href = url;
        a.download = row.name;
        a.click();
      };
      x.onerror = () => {
       // 处理请求失败的情况
          console.log('请求失败');
          this.$Message.warning('下载失败')
      };
      x.send();
    }
  }
};
</script>
<style scoped></style>
